<?php

namespace SunAdmin\Service\FileSystem;

use SunAdmin\Model\AttachmentModel;

class Move extends FilesystemBase
{
    /**
     * 移动文件
     */
    public function move(string $fromPath, string $toPath): array
    {
        try {
            if (!$this->filesystem->exists($fromPath)) {
                throw new \Exception("源文件不存在");
            }

            $moved = $this->filesystem->move($fromPath, $toPath);
            
            if ($moved) {
                // 更新数据库记录
                AttachmentModel::where('path', $fromPath)
                    ->where('disk', $this->currentDisk)
                    ->update([
                        'path' => $toPath,
                        'url' => $this->filesystem->url($toPath)
                    ]);
                
                return [
                    'status' => true,
                    'message' => '文件移动成功',
                    'data' => [
                        'from' => $fromPath,
                        'to' => $toPath,
                        'url' => $this->filesystem->url($toPath)
                    ]
                ];
            } else {
                throw new \Exception("文件移动失败");
            }
        } catch (\Exception $e) {
            return [
                'status' => false,
                'message' => '文件移动失败: ' . $e->getMessage(),
                'data' => []
            ];
        }
    }

    /**
     * 复制文件
     */
    public function copy(string $fromPath, string $toPath): array
    {
        try {
            if (!$this->filesystem->exists($fromPath)) {
                throw new \Exception("源文件不存在");
            }

            $copied = $this->filesystem->copy($fromPath, $toPath);
            
            if ($copied) {
                // 复制数据库记录
                $originalAttachment = AttachmentModel::where('path', $fromPath)
                    ->where('disk', $this->currentDisk)
                    ->find();
                
                if ($originalAttachment) {
                    $newAttachment = $originalAttachment->toArray();
                    unset($newAttachment['id']);
                    $newAttachment['path'] = $toPath;
                    $newAttachment['url'] = $this->filesystem->url($toPath);
                    $newAttachment['add_time'] = date('Y-m-d H:i:s');
                    
                    AttachmentModel::create($newAttachment);
                }
                
                return [
                    'status' => true,
                    'message' => '文件复制成功',
                    'data' => [
                        'from' => $fromPath,
                        'to' => $toPath,
                        'url' => $this->filesystem->url($toPath)
                    ]
                ];
            } else {
                throw new \Exception("文件复制失败");
            }
        } catch (\Exception $e) {
            return [
                'status' => false,
                'message' => '文件复制失败: ' . $e->getMessage(),
                'data' => []
            ];
        }
    }

    /**
     * 重命名文件
     */
    public function rename(string $filePath, string $newName): array
    {
        $pathInfo = pathinfo($filePath);
        $newPath = $pathInfo['dirname'] . '/' . $newName;
        
        return $this->move($filePath, $newPath);
    }
} 